package cn.itcast.web.controller.company;

import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Reference
    private CompanyService companyService;


    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize){

        String companyId = getLoginCompanyId();
        PageInfo<Company> pageInfo = companyService.findByPage(pageNum, pageSize);

        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("company/company-list");

        return mv;
    }

//    @RequestMapping(value = "/save",name = "添加企业")
//    public String save(Date date){
//        int a=1/0;
//        System.out.println(date);
//        return "success";
//    }

    @RequestMapping(value = "/toAdd",name = "新建企业信息")
    public String toAdd(){

        return "company/company-add";
    }

    @RequestMapping(value = "/edit",name = "保存新建企业信息，若Id已存在则更新已有信息")
    public String edit(Company company){

        if (StringUtils.isEmpty(company.getId())){
            companyService.save(company);
        }else {
            companyService.update(company);
        }
        return "redirect:/company/list";
    }

    @RequestMapping(value = "/toUpdate",name = "修改企业信息")
    public ModelAndView toUpdate(String id){

        Company company = companyService.findById(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("company/company-update");
        mv.addObject("company",company);
        return mv;
    }

    @RequestMapping(value = "/delete",name = "删除企业信息")
    public String delete(String id){

        companyService.delete(id);
        return "redirect:/company/list";
    }
}
